//
//  LGDiseaseOverviewController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/13.
//  病情概览

#import "LGDiseaseOverviewController.h"
//患者详情页
#import "LGPatientDetailController_YHJK.h"
#import "LGPatientDetailController2.h"
//随访及医嘱记录
#import "LGFollowAdviceRecordController.h"
//病情描述
#import "LGDiseaseDescriptionController.h"
//照护计划
#import "LGPatientCarePlanController.h"
#import "LGMedicineBallDetailController.h" //详情
//报告中心
#import "LGReportingCenterController.h"
//手术相关报告
#import "LGOutRecordDetailController.h"
//体征监测
#import "LGHomeMonitoringController.h"
//用药方案
#import "LGMedicinePlanController.h"
//用药记录
#import "LGMedicineRecordController.h"
//异常检查项--统计图
#import "LGInspectChartController.h"
//健康评估
#import "LGGetWebViewController.h"
//开具医嘱
#import "LGAddDoctorAdviceController.h"
//添加随访总结
#import "LGMedicineBallFollowUpRecorder.h"
#import "LGCollectionViewLayout.h"
#import "LGDiseaseOverviewItemCell.h"
#import "LGDiseaseOverviewHeaderView.h"
#import "LGDiseaseOverviewEmptyCell.h"
#import "LGDiseaseOverviewFollowCell.h"//随访总结
#import "LGDiseaseOverviewSituatCell.h"//病情描述
#import "LGPatientDetailPlanCell.h"//照护计划
#import "LGDiseaseOverviewDiseaseCell.h"//既往史-共病
#import "LGConsultRecordSurgeryCell.h"//手术
#import "LGDiseaseOverviewMonitorCell.h"//体征监测
#import "LGDiseaseOverviewInspectCell.h"//异常检查
#import "LGDiseaseMedicineDateCell.h"//用药方案、用药记录 - 日期Cell
#import "LGMedicinePlanCell.h"//用药方案
#import "LGMedicineRecordCell2.h"//用药记录
#import "LGDiseaseMedicineChartCell.h"//服药统计图
#import "LGDiseaseEvaluationCell.h"//评估报告
#import "LGPatientNavigationView.h"
#import "LGEvaluationReportModel.h"
#import "SVGKit.h"
#import "SVGKImage.h"
#import "SVGKParser.h"
#import "MCPlayer.h"
@interface LGDiseaseOverviewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,LGDiseaseOverviewMonitorDelegate,LGDiseaseMedicineChartDelegate>
/**   */
@property (nonatomic , strong)LGPatientNavigationView *navView;
/**   */
@property (nonatomic , strong)CAGradientLayer *shadowLayer;
/**  顶部tab栏 */
@property (nonatomic , strong)UICollectionView *collectionView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *titleArray;
/**  随访 */
@property (nonatomic , strong)NSDictionary *followDic;
/**  病情描述 */
@property (nonatomic , strong)NSDictionary *situationDic;
/**  照护计划 */
@property (nonatomic , strong)NSArray *planArray;
/**  共病 */
@property (nonatomic , strong)NSArray *diseaseArray;
/**  手术 */
@property (nonatomic , strong)NSArray *surgeryArray;
/**   */
@property (nonatomic , strong)NSArray *monitorArray;
/**  检验检查 */
@property (nonatomic , strong)NSArray *inspectionArray;
/**  用药方案 */
@property (nonatomic , strong)NSDictionary *medicinePlanDic;
/**  用药记录 */
@property (nonatomic , strong)NSDictionary *medicineRecordDic;
/**  服药统计 */
@property (nonatomic , strong)NSArray *medicineChartArray;
@property (nonatomic , strong)NSDictionary *medicineChartDic;
/**  评估报告 */
@property (nonatomic , strong)NSArray *evaluationArray;
/**   */
@property (nonatomic , assign)NSInteger currentSection;
/**   */
@property (nonatomic , strong)NSArray *emptyArray;
/**   */
@property (nonatomic , assign)CGFloat inspectHeight;
/**   */
@property (nonatomic , copy)NSString *medicChartId;
/**  患者登记号 - 异常项统计用到 */
@property (nonatomic , copy)NSString *patienNum;

@end

@implementation LGDiseaseOverviewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.currentSection = 0;
    self.titleArray = @[@"随访总结",@"病情描述",@"既往史",@"体征监测",@"异常检验指标",@"用药记录",@"评估报告",@"照护计划"];
    self.emptyArray = @[@"暂无随访总结",@"暂无病情描述",@"暂无既往史",@"",@"暂无异常指标(近3个月)",@"暂无用药记录",@"暂无评估报告(近3个月)",@"暂无照护计划"];
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = [UIColor colorWithString:@"#FEFEFF"];
    [self.view.layer addSublayer:self.shadowLayer];
    [self.view addSubview:self.navView];
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.tableView];
    
    [self createBottomView];
    [self.collectionView reloadData];
    [self.tableView reloadData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    [self requestData];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[MCPlayer shareManager] stopMedia];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.Followup/followupSummary" parameters:@{@"patient_id":self.patientid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            //患者信息
            self.navView.patientDic = returnData[@"patient"];
            //概览内容
            self.patienNum = LGNSString(returnData[@"hospital_bano"]);
            self.followDic = returnData[@"followup"];
            self.situationDic = returnData[@"situation"];//判空
            self.planArray = returnData[@"care_tasks"][@"items"];
            self.diseaseArray = returnData[@"visit"][@"diseases"];
            self.surgeryArray = returnData[@"visit"][@"surgery"];
            self.monitorArray = @[returnData[@"symptom"],returnData[@"bp"],returnData[@"pulse"],returnData[@"bmi"],returnData[@"glu"],returnData[@"ac"]];
            self.inspectionArray = returnData[@"abReports"];
            self.medicineChartArray = returnData[@"followupAdvice"][@"chart"];
            self.medicinePlanDic = returnData[@"followupAdvice"][@"advice"];
            self.medicineRecordDic = returnData[@"followupAdvice"][@"followup"];
            if(self.medicineChartArray.count>0){
                [self requestChartDataAction:LGNSString(self.medicineChartArray[0][@"medicine_id"])];
            }
            NSMutableArray *tempArray = [LGEvaluationModel mj_objectArrayWithKeyValuesArray:returnData[@"evas"]];
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                for (NSInteger i=0; i<tempArray.count; i++) {
                    LGEvaluationModel *model = tempArray[i];
                    model.image = [SVGKImage imageWithContentsOfURL:[NSURL URLWithString:model.icon]].UIImage;
                    [tempArray replaceObjectAtIndex:i withObject:model];
                }
                self.evaluationArray = tempArray;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                });
            });
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

//请求药物统计图
-(void)requestChartDataAction:(NSString *)medicinId{
    self.medicChartId = medicinId;
    [RequestUtil withPOST:@"/doctor.Medicine/chartsdata" parameters:@{@"patient_id":@"4",@"medicine_id":medicinId} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.medicineChartDic = returnData;
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

//患者详情
-(void)checkPatientDetailAction{
    if ([self.diseaseCode isEqualToString:@"24"] || [self.diseaseCode isEqualToString:@"25"]) {
        LGPatientDetailController2 *controller = [[LGPatientDetailController2 alloc]init];
        controller.patientid = self.patientid;
        controller.levelid = self.levelid;
        controller.diseaseCode = self.diseaseCode;
        [self.navigationController pushViewController:controller animated:YES];
        
    }else{
        LGPatientDetailController_YHJK *controller = [[LGPatientDetailController_YHJK alloc]init];
        controller.patientid = self.patientid;
        controller.levelid = self.levelid;
        controller.diseaseCode = self.diseaseCode;
        [self.navigationController pushViewController:controller animated:YES];
    }
}

//点击区头
-(void)headerViewTouched:(UIButton *)sender{
    NSInteger index = sender.tag-1000;
    if(index == 0){
        //随访总结
        LGFollowAdviceRecordController *controller = [[LGFollowAdviceRecordController alloc]init];
        controller.patientid = self.patientid;
        controller.currentInex = 2;
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if (index == 1){
        //病情描述
        LGDiseaseDescriptionController *controller = [[LGDiseaseDescriptionController alloc]init];
        controller.patientid = self.patientid;
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if (index == 2){
        //既往史 -- 诊疗记录
        LGReportingCenterController *controller = [[LGReportingCenterController alloc]init];
        controller.patientid = self.patientid;
        controller.diseaseCode = self.diseaseCode;
        controller.currentIndex = 2;
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if (index == 4){
        //异常指标
        LGReportingCenterController *controller = [[LGReportingCenterController alloc]init];
        controller.patientid = self.patientid;
        controller.diseaseCode = self.diseaseCode;
        controller.currentIndex = 5;
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if (index == 6){
        //评估报告
        LGReportingCenterController *controller = [[LGReportingCenterController alloc]init];
        controller.patientid = self.patientid;
        controller.diseaseCode = self.diseaseCode;
        controller.currentIndex = 4;
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if (index == 7){
        //照护计划
        LGPatientCarePlanController *controller = [[LGPatientCarePlanController alloc]init];
        controller.diseaseCode = self.diseaseCode;
        controller.patientid = self.patientid;
        [self.navigationController pushViewController:controller animated:YES];
    }
}


//查看用药记录
-(void)checkMedicineRecordAction{
    LGMedicineRecordController *controller = [[LGMedicineRecordController alloc]init];
    controller.patientid = self.patientid;
    [self.navigationController pushViewController:controller animated:YES];
}

//体征监测
-(void)checkMonitorDetail:(NSInteger)index{
    LGHomeMonitoringController *controller = [[LGHomeMonitoringController alloc]init];
    controller.diseaseCode = self.diseaseCode;
    controller.patientid = self.patientid;
    controller.currentIndex = index;
    [self.navigationController pushViewController:controller animated:YES];
}

//调整照护计划
-(void)adjustCarePlanAction{
    LGPatientCarePlanController *controller = [[LGPatientCarePlanController alloc]init];
    controller.patientid = self.patientid;
    controller.diseaseCode = self.diseaseCode;
    [self.navigationController pushViewController:controller animated:YES];
}

//开具医嘱
-(void)adviceBtnTouchAction{
    LGAddDoctorAdviceController *controller = [[LGAddDoctorAdviceController alloc]init];
    controller.patientid = self.patientid;
    controller.followid = @"";
    [self.navigationController pushViewController:controller animated:YES];
}

//添加随访总结
-(void)addFollowUpAction{
    LGMedicineBallFollowUpRecorder *controller = [[LGMedicineBallFollowUpRecorder alloc]init];
    controller.planid = @"0";
    controller.patientid = self.patientid;
    [self.navigationController pushViewController:controller animated:YES];
}

#pragma mark
#pragma mark ====> collectionView
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.titleArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LGDiseaseOverviewItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.content = self.titleArray[indexPath.item];
    cell.isSelected = (indexPath.item == self.currentSection)?YES:NO;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.currentSection = indexPath.item;
    [self.collectionView reloadData];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.item] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *title = LGNSString(self.titleArray[indexPath.item]);
    CGFloat width = [title textWidthWithFont:LGFontWeight(15, UIFontWeightMedium) maxHeight:MAXFLOAT]+viewPix(5);
    return CGSizeMake(width, viewPix(32));
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titleArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0 && self.followDic.count>0){
        //随访总结
        return 1;
        
    }else if (section == 1 && ![NSObject isNullOrNilWithObject:self.situationDic]){
        //病情描述
        return 1;
        
    }else if (section == 2 && (self.surgeryArray.count>0 || self.diseaseArray.count>0)){
        //既往史
        NSInteger diseaseCount = (self.diseaseArray.count>0)?1:0;
        return self.surgeryArray.count+diseaseCount;
        
    }else if (section == 3){
        //体征监测
        return 1;
        
    }else if (section == 4 && self.inspectionArray.count>0){
        //异常指标
        return 1;
        
    }else if (section == 5 && (([self.medicineRecordDic isKindOfClass:[NSDictionary class]] && self.medicineRecordDic.count>0) || self.medicineChartArray.count>0)){
        //用药记录
        NSArray *medicineArray = ([self.medicineRecordDic isKindOfClass:[NSDictionary class]] && self.medicineRecordDic.count>0)?self.medicineRecordDic[@"medicines"]:@[];
        NSInteger medicineCount = (medicineArray.count>0)?medicineArray.count+1:0;
        NSInteger chatCount = ([self.medicineRecordDic isKindOfClass:[NSDictionary class]] && self.medicineChartArray.count>0)?1:0;
        return chatCount+medicineCount;
        
    }else if(section == 6 && self.evaluationArray.count>0){
        //评估报告
        return self.evaluationArray.count;
        
    }else if (section == 7 && self.planArray.count>0){
        //照护计划
        return self.planArray.count;
        
    }
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0 && self.followDic.count>0){
        //随访总结
        LGDiseaseOverviewFollowCell *followCell = [tableView dequeueReusableCellWithIdentifier:@"followCell" forIndexPath:indexPath];
        followCell.selectionStyle = UITableViewCellSelectionStyleNone;
        followCell.dataDic = self.followDic;
        return followCell;
        //LGDiseaseOverviewSituatCell
    }else if (indexPath.section == 1 && ![NSObject isNullOrNilWithObject:self.situationDic]){
        //病情描述
        LGDiseaseOverviewSituatCell *situationCell = [tableView dequeueReusableCellWithIdentifier:@"situationCell" forIndexPath:indexPath];
        situationCell.selectionStyle = UITableViewCellSelectionStyleNone;
        situationCell.dataDic = self.situationDic;
        return situationCell;
        
    }else if (indexPath.section == 2){
        //既往史
        NSInteger first = (self.diseaseArray.count>0)?1:0;
        if(self.diseaseArray.count >0 && indexPath.row == 0){
            LGDiseaseOverviewDiseaseCell *diseaseCell = [tableView dequeueReusableCellWithIdentifier:@"diseaseCell" forIndexPath:indexPath];
            diseaseCell.selectionStyle = UITableViewCellSelectionStyleNone;
            diseaseCell.dataArray = self.diseaseArray;
            return diseaseCell;
            
        }else if(self.surgeryArray.count>0){
            LGConsultRecordSurgeryCell *surgeryCell = [tableView dequeueReusableCellWithIdentifier:@"surgeryCell" forIndexPath:indexPath];
            surgeryCell.selectionStyle = UITableViewCellSelectionStyleNone;
            surgeryCell.content = LGNSString(self.surgeryArray[indexPath.row-first]);
            return surgeryCell;
        }
        
    }else if (indexPath.section == 3){
        //体征监测
        LGDiseaseOverviewMonitorCell *monitorCell = [tableView dequeueReusableCellWithIdentifier:@"monitorCell" forIndexPath:indexPath];
        monitorCell.selectionStyle = UITableViewCellSelectionStyleNone;
        monitorCell.dataArray = self.monitorArray;
        monitorCell.delegate = self;
        return monitorCell;
        
    }else if (indexPath.section == 4 && self.inspectionArray.count>0){
        //异常指标
        LGDiseaseOverviewInspectCell *inspectCell = [tableView dequeueReusableCellWithIdentifier:@"inspectCell" forIndexPath:indexPath];
        inspectCell.selectionStyle = UITableViewCellSelectionStyleNone;
        inspectCell.fatherTableView = self.tableView;
        inspectCell.dataArray = self.inspectionArray;
        //跳转到指标统计图
        inspectCell.checkInspectChart = ^(NSDictionary * _Nonnull tempDic) {
            LGInspectChartController *controller = [[LGInspectChartController alloc]init];
            controller.code = LGNSString(tempDic[@"item_code"]);
            controller.patientid = self.patientid;
            controller.patientNum = self.patienNum;
            [self.navigationController pushViewController:controller animated:YES];
        };
        return inspectCell;
        
    }else if (indexPath.section == 5 && (([self.medicineRecordDic isKindOfClass:[NSDictionary class]] && self.medicineRecordDic.count>0) || self.medicineChartArray.count>0)){
        //用药记录
        NSArray *medicineArray = ([self.medicineRecordDic isKindOfClass:[NSDictionary class]] && self.medicineRecordDic.count>0)?self.medicineRecordDic[@"medicines"]:@[];
        if(medicineArray.count > 0 && indexPath.row == 0){
            LGDiseaseMedicineDateCell *dateCell = [tableView dequeueReusableCellWithIdentifier:@"dateCell" forIndexPath:indexPath];
            dateCell.selectionStyle = UITableViewCellSelectionStyleNone;
            dateCell.dataDic = self.medicineRecordDic;
            [dateCell.checkBtn addTarget:self action:@selector(checkMedicineRecordAction) forControlEvents:UIControlEventTouchUpInside];
            return dateCell;
            
        }else if(medicineArray.count > 0 && indexPath.row <= medicineArray.count){
            LGMedicineRecordCell2 *recordCell = [tableView dequeueReusableCellWithIdentifier:@"medicinRecordCell" forIndexPath:indexPath];
            recordCell.selectionStyle = UITableViewCellSelectionStyleNone;
            recordCell.type = @"middle";
            recordCell.dataDic = medicineArray[indexPath.row-1];
            return recordCell;
        }else{
            //统计图
            LGDiseaseMedicineChartCell *chartCell = [tableView dequeueReusableCellWithIdentifier:@"chartCell" forIndexPath:indexPath];
            chartCell.selectionStyle = UITableViewCellSelectionStyleNone;
            if(![chartCell.medicineid isEqualToString:self.medicChartId]){
                chartCell.medicineid = self.medicChartId;
                chartCell.medicineArray = self.medicineChartArray;
                chartCell.chartDic = self.medicineChartDic;
            }
            chartCell.delegate = self;
            return chartCell;
        }

    }else if(indexPath.section == 6 && self.evaluationArray.count>0){
        //评估报告
        LGDiseaseEvaluationCell *evaCell = [tableView dequeueReusableCellWithIdentifier:@"evaCell" forIndexPath:indexPath];
        evaCell.selectionStyle = UITableViewCellSelectionStyleNone;
        evaCell.model = self.evaluationArray[indexPath.row];
        return evaCell;
        
    }else if (indexPath.section == 7 && self.planArray.count>0){
        //照护计划
        LGPatientDetailPlanCell *planCell = [tableView dequeueReusableCellWithIdentifier:@"planCell" forIndexPath:indexPath];
        planCell.selectionStyle = UITableViewCellSelectionStyleNone;
        planCell.dataDic = self.planArray[indexPath.row];
        return planCell;
        
    }
    LGDiseaseOverviewEmptyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"emptyCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.content = self.emptyArray[indexPath.section];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2){
        //既往史 -- 手术相关报告
        NSInteger first = (self.diseaseArray.count>0)?1:0;
        if(self.surgeryArray.count>0){
            LGOutRecordDetailController *controller = [[LGOutRecordDetailController alloc]init];
            controller.recordid = @"";
            controller.patientid = self.patientid;
            controller.reportDate = LGNSString(self.surgeryArray[indexPath.row-first][@"date"]);
            [self.navigationController pushViewController:controller animated:YES];
        }

    }else if (indexPath.section == 6 && self.evaluationArray.count>0){
        //评估
        LGEvaluationModel *model = self.evaluationArray[indexPath.row];
        LGGetWebViewController *controller = [[LGGetWebViewController alloc]init];
        controller.withAsset = YES;
        controller.urlStr = model.h5url;
        [self.navigationController pushViewController:controller animated:YES];
        
    }else if(indexPath.section == 7 && self.planArray.count>0) {
        //照护计划
        NSDictionary *tempDic = self.planArray[indexPath.row];
        NSString *type = LGNSString(tempDic[@"task_type"]);
        //二期手术、冠脉造影、再次入院 可点
        if([type isEqualToString:@"2"]||[type isEqualToString:@"3"]||[type isEqualToString:@"4"]){
            LGMedicineBallDetailController *controller = [[LGMedicineBallDetailController alloc]init];
            controller.operatName = LGNSString(tempDic[@"type_name"]);
            controller.planid = LGNSString(tempDic[@"hospital_plan_id"]);
            [self.navigationController pushViewController:controller animated:YES];
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if((section == 0 && self.followDic.count == 0) ||
       (section == 1 && [NSObject isNullOrNilWithObject:self.situationDic]) ||
       (section == 5 && (![self.medicineRecordDic isKindOfClass:[NSDictionary class]] || self.medicineRecordDic.count == 0)) ||
       (section == 6 && self.evaluationArray.count == 0)||
       (section == 7 && self.planArray.count == 0)){
        //数组为空
        return viewPix(56);
        
    }else if (section == 4){
        //异常指标
        return (self.inspectionArray.count>0)?viewPix(72):viewPix(70);
        
    }else {
        //普通 - 有数据
        return viewPix(56);
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    CGFloat height = viewPix(56);
    BOOL showTip = YES;
    if((section == 0 && self.followDic.count == 0) ||
       (section == 1 && [NSObject isNullOrNilWithObject:self.situationDic]) ||
       (section == 5 && (![self.medicinePlanDic isKindOfClass:[NSDictionary class]] || self.medicinePlanDic.count == 0)) ||
       (section == 5 && ((![self.medicinePlanDic isKindOfClass:[NSDictionary class]] || self.medicineRecordDic.count == 0) && self.medicineChartArray.count == 0)) ||
       (section == 6 && self.evaluationArray.count == 0)||
       (section == 7 && self.planArray.count == 0)){
        //数组为空
        height = viewPix(56);
        showTip = NO;
        
    }else if (section == 4){
        //异常指标
        showTip = (self.inspectionArray.count>0)?YES:NO;
        height = (self.inspectionArray.count>0)?viewPix(72):viewPix(70);
    }
    NSArray *subTitles = @[@"查看全部",@"历史记录",@"诊疗记录",@"",@"查看核心指标",@"",@"查看全部",@"查看全部"];
    LGDiseaseOverviewHeaderView *headerView = [[LGDiseaseOverviewHeaderView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, height)];
    headerView.showTip = showTip;
    headerView.title = self.titleArray[section];
    headerView.subTitle = subTitles[section];
    headerView.actionBtn.tag = 1000+section;
    [headerView.actionBtn addTarget:self action:@selector(headerViewTouched:) forControlEvents:UIControlEventTouchUpInside];
    
    if(section == 4){
        UILabel *descLabel = [UILabel lableWithText:@"点击指标可查看历史趋势变化" colorString:@"#999999" textFont:LGFont(12) textAlignment:NSTextAlignmentLeft lines:1];
        descLabel.frame = CGRectMake(viewPix(30), viewPix(48), Screen_W-viewPix(60), viewPix(20));
        [headerView addSubview:descLabel];
    }
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(18);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    LGCornerFooterView *footView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(18))];
    footView.backgroundColor = [UIColor clearColor];
    footView.bottomMargin = viewPix(8);
    return footView;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.tableView) {
        CGFloat offsetY = self.tableView.contentOffset.y;
        for (NSInteger i=0; i<self.tableView.numberOfSections; i++) {
            CGRect sectionRect = [self.tableView rectForSection:i];
            if(sectionRect.origin.y >= offsetY){
                if(self.currentSection != i){
                    self.currentSection = MAX(0, i-1);
                    [self.collectionView reloadData];
//                    CGFloat leftMargin = 0;
                    if(i<=2){
                        [self.collectionView setContentOffset:CGPointMake(-viewPix(12), 0)];
                        
                    }else if(i>2 && i<self.titleArray.count-2){
                        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:i-2 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
                    }else{
                        if(isPad){
                            [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:i-2 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
                        }else{
                            [self.collectionView setContentOffset:CGPointMake(self.collectionView.contentSize.width-Screen_W, 0)];
                        }
                    }
                }
                return;
            }
        }
    }
}

-(LGPatientNavigationView *)navView{
    if(!_navView){
        _navView = [[LGPatientNavigationView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, topBarHeight)];
        _navView.backgroundColor = [UIColor colorWithString:@"#E5F3FF"];
        [_navView.backButton addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [_navView.checkButton addTarget:self action:@selector(checkPatientDetailAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _navView;
}

-(CAGradientLayer *)shadowLayer{
    if(!_shadowLayer){
        _shadowLayer = [CAGradientLayer layer];
        _shadowLayer.frame = CGRectMake(0,topBarHeight,Screen_W,topBarHeight+viewPix(115));
        _shadowLayer.startPoint = CGPointMake(0.5, 0);
        _shadowLayer.endPoint = CGPointMake(0.5, 1);
        _shadowLayer.colors = @[(__bridge id)[UIColor colorWithString:@"#E5F3FF"].CGColor, (__bridge id)[UIColor colorWithString:@"#F3F9FE"].CGColor, (__bridge id)[UIColor colorWithString:@"#FEFEFF"].CGColor];
        _shadowLayer.locations = @[@(0), @(0.4f), @(1.0f)];
    }
    return _shadowLayer;
}

-(UICollectionView *)collectionView{
    if(!_collectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumInteritemSpacing = viewPix(12);
        layout.minimumLineSpacing = viewPix(12);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, topBarHeight, Screen_W, viewPix(35)) collectionViewLayout:layout];
        [_collectionView registerClass:[LGDiseaseOverviewItemCell class] forCellWithReuseIdentifier:@"cell"];
        _collectionView.contentInset = UIEdgeInsetsMake(0, viewPix(10), 0, viewPix(10));
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
    }
    return _collectionView;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topBarHeight+viewPix(40), Screen_W, Screen_H-topBarHeight-viewPix(40)-viewPix(60)-bottomSafeBarHeight/4.0) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGDiseaseOverviewEmptyCell class] forCellReuseIdentifier:@"emptyCell"];
        [_tableView registerClass:[LGDiseaseOverviewSituatCell class] forCellReuseIdentifier:@"situationCell"];
        [_tableView registerClass:[LGPatientDetailPlanCell class] forCellReuseIdentifier:@"planCell"];
        [_tableView registerClass:[LGDiseaseOverviewDiseaseCell class] forCellReuseIdentifier:@"diseaseCell"];
        [_tableView registerClass:[LGConsultRecordSurgeryCell class] forCellReuseIdentifier:@"surgeryCell"];
        [_tableView registerClass:[LGDiseaseOverviewMonitorCell class] forCellReuseIdentifier:@"monitorCell"];
        [_tableView registerClass:[LGDiseaseOverviewInspectCell class] forCellReuseIdentifier:@"inspectCell"];
        [_tableView registerClass:[LGDiseaseMedicineDateCell class] forCellReuseIdentifier:@"dateCell"];
        [_tableView registerClass:[LGMedicinePlanCell class] forCellReuseIdentifier:@"medicinPlanCell"];
        [_tableView registerClass:[LGMedicineRecordCell2 class] forCellReuseIdentifier:@"medicinRecordCell"];
        [_tableView registerClass:[LGDiseaseMedicineChartCell class] forCellReuseIdentifier:@"chartCell"];
        [_tableView registerClass:[LGDiseaseEvaluationCell class] forCellReuseIdentifier:@"evaCell"];
        [_tableView registerClass:[LGDiseaseOverviewFollowCell class] forCellReuseIdentifier:@"followCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

-(void)createBottomView{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, kMaxY(self.tableView.frame), Screen_W, viewPix(60)+bottomSafeBarHeight/4.0)];
    baseView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:baseView];
    
    CGFloat itemW = (Screen_W-viewPix(32)-viewPix(12))/3.0;
    CGFloat itemH = viewPix(43);
    UIButton *planBtn = [UIButton buttonWithTitle:@"调整照护计划" titleFont:LGFontWeight(13, UIFontWeightMedium) textColor:@"#3291FF" imageName:@""];
    planBtn.frame = CGRectMake(viewPix(16), viewPix(8), itemW, itemH);
    planBtn.layer.borderColor = [UIColor colorWithString:@"#3291FF"].CGColor;
    planBtn.layer.borderWidth = 1;
    planBtn.cornerRidus = 10;
    [planBtn addTarget:self action:@selector(adjustCarePlanAction) forControlEvents:UIControlEventTouchUpInside];
    [baseView addSubview:planBtn];
    
    UIButton *adviceBtn = [UIButton buttonWithTitle:@"开具医嘱" titleFont:LGFontWeight(13, UIFontWeightMedium) textColor:@"#3291FF" imageName:@""];
    adviceBtn.frame = CGRectMake(kMaxX(planBtn.frame)+viewPix(6), viewPix(8), itemW, itemH);
    adviceBtn.layer.borderColor = [UIColor colorWithString:@"#3291FF"].CGColor;
    adviceBtn.layer.borderWidth = 1;
    adviceBtn.cornerRidus = 10;
    [adviceBtn addTarget:self action:@selector(adviceBtnTouchAction) forControlEvents:UIControlEventTouchUpInside];
    [baseView addSubview:adviceBtn];
    
    UIButton *followBtn = [UIButton buttonWithTitle:@"添加随访总结" titleFont:LGFontWeight(13, UIFontWeightMedium) textColor:@"#FFFFFF" imageName:@""];
    followBtn.frame = CGRectMake(kMaxX(adviceBtn.frame)+viewPix(6), viewPix(8), itemW, itemH);
    followBtn.backgroundColor = [UIColor colorWithString:@"#3291FF"];
    followBtn.cornerRidus = 10;
    [followBtn addTarget:self action:@selector(addFollowUpAction) forControlEvents:UIControlEventTouchUpInside];
    [baseView addSubview:followBtn];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
